/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simulateurs.view.controllersView;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import simulateurs.model.monde.Equipe;
import simulateurs.view.I_View;
import simulateurs.view.View;

/**
 *
 * @author Lunesco
 */
public class ButtonsPanelView extends JPanel implements I_View {
	private JButton demarrer=null;
	private JButton pauser=null;
	private JButton stopper=null;
	private JButton reset=null;
	private JButton accelerer=null;
	private JButton ralentir=null;
	private View parentView=null;
	private boolean pauseClicked=false;

	public ButtonsPanelView(View parentView) {	
		this.parentView=parentView;
		initButtonsPanelView();
	}
	
	private void initButtonsPanelView(){
		this.setLayout(new GridLayout(1,4));
		
		demarrer=new JButton("Start");
		demarrer.setFont(new Font("Tahoma", 0, 20));
		demarrer.setBorder(new LineBorder(Color.WHITE, 1));
		demarrer.setForeground(new Color(0xffffff));
		demarrer.setBackground(new Color(0x489620));
		demarrer.requestFocus();
		demarrer.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				performDemarrer();
			}
		});
		this.add(demarrer);
		
		pauser=new JButton("Pause");
		pauser.setFont(new Font("Tahoma", 0, 20));
		pauser.setBorder(new LineBorder(Color.WHITE, 1));
		pauser.setEnabled(false);
		pauser.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				performPauser();
			}
		});
		this.add(pauser);
		
		stopper=new JButton("Stop");
		stopper.setFont(new Font("Tahoma", 0, 20));
		stopper.setBorder(new LineBorder(Color.WHITE, 1));
		stopper.setEnabled(false);
		stopper.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				performStopper();
			}
		});
		this.add(stopper);
		
		reset=new JButton("Reset");
		reset.setFont(new Font("Tahoma", 0, 20));
		reset.setBorder(new LineBorder(Color.WHITE, 1));
		reset.setEnabled(false);
		reset.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				performReset();
			}
		});
		this.add(reset);
		
		accelerer=new JButton("Speed +");
		accelerer.setFont(new Font("Tahoma", 0, 20));
		accelerer.setBorder(new LineBorder(Color.WHITE, 1));
		accelerer.setEnabled(false);
		accelerer.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				performAccelerer();
			}
		});
		this.add(accelerer);
		
		ralentir=new JButton("Speed -");
		ralentir.setFont(new Font("Tahoma", 0, 20));
		ralentir.setBorder(new LineBorder(Color.WHITE, 1));
		ralentir.setEnabled(false);
		ralentir.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				performRalentir();
			}
		});
		this.add(ralentir);
	}

	public View getParentView() {
		return parentView;
	}
	
	@Override
	public void repeindre() {
		this.repaint();
	}
	
	public void performAccelerer(){
		this.parentView.getModel().getScheduler().getHorloge().accelerer();
	}
	
	public void performRalentir(){
		this.parentView.getModel().getScheduler().getHorloge().ralentir();
	}
	
	public void performDemarrer(){
		this.parentView.getModel().getScheduler().setArret(false);
		this.demarrer.setEnabled(false);
		this.demarrer.setForeground(null);
		this.demarrer.setBackground(null);
		this.pauser.requestFocus();
		this.pauser.setEnabled(true);
		this.pauser.setForeground(new Color(0xffffff));
		this.pauser.setBackground(new Color(0xEC870E));
		this.pauser.setText("Pause");
		this.pauseClicked=false;
		this.stopper.setEnabled(true);
		this.stopper.setForeground(new Color(0xffffff));
		this.stopper.setBackground(new Color(0xC82E31));
		this.reset.setEnabled(true);
		this.reset.setForeground(new Color(0xffffff));
		this.reset.setBackground(new Color(0x184785));
		this.accelerer.setEnabled(true);
		this.accelerer.setBackground(new Color(0xA2007C));
		this.accelerer.setForeground(new Color(0xffffff));
		this.ralentir.setEnabled(true);
		this.ralentir.setBackground(new Color(0x00B2BF));
		this.ralentir.setForeground(new Color(0xffffff));
		this.parentView.getRootPane().setDefaultButton(this.pauser);
        this.parentView.getModel().getArene().getTimer().start();
	}
	
	public void performPauser(){
		if(pauseClicked) {
			this.parentView.getModel().getScheduler().setArret(false);
			this.pauseClicked=false;
			this.pauser.setText("Pause");
			this.accelerer.setEnabled(true);
			this.accelerer.setBackground(new Color(0xA2007C));
			this.accelerer.setForeground(new Color(0xffffff));
			this.ralentir.setEnabled(true);
			this.ralentir.setBackground(new Color(0x00B2BF));
			this.ralentir.setForeground(new Color(0xffffff));
            this.parentView.getModel().getArene().getTimer().pause();
		}
		else{
			this.parentView.getModel().getScheduler().setArret(true);
			this.pauseClicked=true;
			this.pauser.setText("Continue");
			this.accelerer.setEnabled(false);
			this.accelerer.setBackground(null);
			this.accelerer.setForeground(null);
			this.ralentir.setEnabled(false);
			this.ralentir.setBackground(null);
			this.ralentir.setForeground(null);
            this.parentView.getModel().getArene().getTimer().pause();
		}
		this.parentView.getRootPane().setDefaultButton(this.pauser);
	}
	
	public void performStopper(){
		this.parentView.getModel().getScheduler().setArret(true);
		this.stopper.setEnabled(false);
		this.stopper.setForeground(null);
		this.stopper.setBackground(null);
		this.reset.requestFocus();
		this.pauser.setEnabled(false);
		this.pauser.setForeground(null);
		this.pauser.setBackground(null);
		this.accelerer.setEnabled(false);
		this.accelerer.setBackground(null);
		this.accelerer.setForeground(null);
		this.ralentir.setEnabled(false);
		this.ralentir.setBackground(null);
		this.ralentir.setForeground(null);
		this.pauser.setText("Pause");
		this.parentView.getRootPane().setDefaultButton(this.demarrer);
        this.parentView.getModel().getArene().getTimer().stop();
		String finalScore="";
		for(int i=0; i<this.parentView.getModel().getArene().getListeEquipes().size(); i++){
			Equipe e=this.parentView.getModel().getArene().getListeEquipes().get(i);
			finalScore+=e.getNom()+": "+e.getScore()+"\n";
		}
		String finalTime="Final Time : "+this.parentView.getModel().getArene().getTimer().getFinalTime()+" seconds\n\n";
		String finalInfo=finalTime+"Final Score :\n\n"+finalScore;
		JOptionPane.showMessageDialog(this.parentView, finalInfo, "Game Over", JOptionPane.INFORMATION_MESSAGE);
                this.parentView.getModel().getArene().setFinDuJeu(true);
        }
	
	public void performReset(){
		if(this.stopper.isEnabled()) this.performStopper();
		this.demarrer.requestFocus();
		this.demarrer.setEnabled(true);
		this.demarrer.setForeground(new Color(0xffffff));
		this.demarrer.setBackground(new Color(0x489620));
		this.reset.setEnabled(false);
		this.reset.setForeground(null);
		this.reset.setBackground(null);
		this.parentView.getModel().initMonde();
	}

	public JButton getDemarrer() {
		return demarrer;
	}

	public boolean isPauseClicked() {
		return pauseClicked;
	}

	public JButton getPauser() {
		return pauser;
	}

	public JButton getReset() {
		return reset;
	}

	public JButton getStopper() {
		return stopper;
	}
}
